package com.yupi.springbootinit;

import cn.hutool.core.io.FileUtil;
import com.yupi.springbootinit.utils.file.FileCopy;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

import java.io.*;
import java.nio.file.Path;

public class SMBExample {

    public static void main(String[] args) {
        File file = new File("smb://192.168.7.108/B/B巴音郭楞职业技术学院/巴音郭楞职业技术学院BYGLZYJSXYecology20220620-001升级包说明文档.doc");
        String username = "test"; // 替换为实际的 Windows 用户名
        String password = "test"; // 替换为实际的密码
        String sharedFolder = "smb://192.168.7.108/ECOLOGY_customer/B/B巴音郭楞职业技术学院/巴音郭楞职业技术学院BYGLZYJSXYecology20220620-001升级包说明文档.doc"; // 替换为实际的共享文件夹路径
        try {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
            SmbFile smbFile = new SmbFile(sharedFolder, auth);
            if (smbFile.exists()) {
                String property = System.getProperty("user.dir");
                File localFile = new File(property+"/ttt.doc"); // 替换为保存到本地的文件路径
                try (InputStream inputStream = new BufferedInputStream(new SmbFileInputStream(smbFile));
                     OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile))) {

                    // 从 SMB 文件中读取内容，并写入到本地文件中
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
                    System.out.println("File downloaded to: " + localFile.getAbsolutePath());
                }
            } else {
                System.out.println("Shared folder does not exist.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
